package org.rpgmapper.view;

import org.rpgmapper.PlacePlayersActivity;
import org.rpgmapper.R;
import org.rpgmapper.bo.Bonus;
import org.rpgmapper.bo.BonusType;
import org.rpgmapper.skirmish.SkirmishManager;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;


public class SkirmishInfoView extends FrameLayout{

	private float TRANSLATION_Y;
	
	private View view;
	private boolean isVisible = false;
	
	public SkirmishInfoView(Context context) {
		super(context);
		LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		/*glView = */inflater.inflate(R.layout.skirmish_info, this);
		init();
	}

	public SkirmishInfoView(Context context, AttributeSet attrs) {
		super(context, attrs);
		LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		/*glView = */inflater.inflate(R.layout.skirmish_info, this);
		init();
	}
	
	private void init() {
		findViewById(R.id.new_bonus).measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
		TRANSLATION_Y = findViewById(R.id.new_bonus).getMeasuredHeight();
		this.setTranslationY(TRANSLATION_Y);
        /*findViewById(R.id.new_map).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
				builder.setMessage("Are you sure you want to create new map ?")
				       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
				           public void onClick(DialogInterface dialog, int id) {
								findViewById(R.id.menu_create_map).setVisibility(View.VISIBLE);
				           }
				       })
				       .setNegativeButton("No", new DialogInterface.OnClickListener() {
				           public void onClick(DialogInterface dialog, int id) {
				                dialog.cancel();
				           }
				       });
				AlertDialog alert = builder.create();
				alert.show();
			}
		});*/
        
        findViewById(R.id.place_players).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(getContext(), PlacePlayersActivity.class);
				getContext().startActivity(intent);
			}
		});
        
        findViewById(R.id.start_skirmish).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if (isVisible) {
					SkirmishInfoView.this.toggle();
				}
				SkirmishManager.getInstance().beginSkirmish();
			}
		});
        
        findViewById(R.id.character_info).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				SkirmishManager.getInstance().showCharacterInfo();
			}
		});
        
        findViewById(R.id.stop_skirmish).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				SkirmishManager.getInstance().endSkirmish();
			}
		});
        
        /*findViewById(R.id.new_round).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				SkirmishManager.getInstance().nextRound();
			}
		});*/
        
        findViewById(R.id.next_character).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				SkirmishManager.getInstance().nextCharacter();
			}
		});

        findViewById(R.id.add_character).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				SkirmishManager.getInstance().wantToAddCharacter();
			}
		});

        findViewById(R.id.round_info).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				SkirmishInfoView.this.toggle();
			}
		});
        findViewById(R.id.bonus_info).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				SkirmishInfoView.this.toggle();
			}
		});
        
        ((TextView)findViewById(R.id.duration)).setText(((SeekBar)findViewById(R.id.seekBarDuration)).getProgress() + " round(s)");
        
        ((SeekBar)findViewById(R.id.seekBarDuration)).setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			public void onStopTrackingTouch(SeekBar seekBar) {
			}
			public void onStartTrackingTouch(SeekBar seekBar) {
			}
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				((TextView)findViewById(R.id.duration)).setText(progress + " round(s)");
			}
		});

        findViewById(R.id.ok).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				int radioId = ((RadioGroup)findViewById(R.id.bonus_type)).getCheckedRadioButtonId();
				Bonus bonus = new Bonus(((SeekBar)findViewById(R.id.seekBarDuration)).getProgress(),
						((TextView)findViewById(R.id.effect)).getText().toString(),
						(radioId == R.id.hero)?BonusType.CHARACTER:BonusType.MONSTER);
				SkirmishManager.getInstance().addBonus(bonus);
				SkirmishInfoView.this.toggle();
			}
		});
	}
	
	public void toggle() {
		if (isVisible) {
			ObjectAnimator alpha = ObjectAnimator.ofFloat(this, "translationY", this.getTranslationY()+TRANSLATION_Y);
			alpha.setDuration(100);
			alpha.start();
			isVisible = false;
		} else {
			ObjectAnimator alpha = ObjectAnimator.ofFloat(this, "translationY", this.getTranslationY()-TRANSLATION_Y);
			alpha.setDuration(300);
			alpha.start();
			isVisible = true;
		}
	}
	
	public void hide() {
		if (isVisible) {
			ObjectAnimator alpha = ObjectAnimator.ofFloat(this, "alpha", this.getTranslationY()-TRANSLATION_Y);
			alpha.setDuration(100);
			alpha.start();
			isVisible = false;
		}
	}
	
	public void updateRoundInfos() {
		((TextView)findViewById(R.id.round_info)).setText(SkirmishManager.getInstance().getCharactersAsText());
	}
	
	public void updateBonusInfos() {
		((TextView)findViewById(R.id.bonus_info_characters)).setText(SkirmishManager.getInstance().getCharactersBonusAsText());
		((TextView)findViewById(R.id.bonus_info_monsters)).setText(SkirmishManager.getInstance().getMonstersBonusAsText());
	}
	
	public int getVisibleHeight() {
		findViewById(R.id.text).measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
		return findViewById(R.id.text).getMeasuredHeight();
	}

   
}
